<?php
/*
 * Created on Feb 7, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 	$title = 'Loại bất động sản';
 	include('../includes/session.php');
	if($session->logged_in){
		if($session->userlevel == 1){
			header("Location: ../index.php");
		}
	}else{
		header("Location: login.php");
	}
	include('../includes/encrypturl.php');
	include('../models/cityregion_model.php');
	$session->setHeader(10);	
 	include('../includes/header.inc');
	
	if (isset($_GET['SelectedCity'])){
		$SelectedCity =strtoupper($_GET['SelectedCity']);
	} elseif(isset($_POST['SelectedCity'])){
		$SelectedCity =strtoupper($_POST['SelectedCity']);
	}
	if (isset($_GET['SelectedDistrict'])){
		$SelectedDistrict =strtoupper($_GET['SelectedDistrict']);
	} elseif(isset($_POST['SelectedDistrict'])){
		$SelectedDistrict =strtoupper($_POST['SelectedDistrict']);
	}
	
	echo '<div id="wrapper">';
	echo '<div id="content">';
	if (!isset($SelectedDistrict) || (isset($SelectedDistrict) && isset($_GET['delete']))) {	
		echo '<div id="box">';
		
		if (isset($_GET['deleteCity']) && isset($SelectedCity)) {
			if($cityModel->deleteCity($SelectedCity) == 0){
				echo prnMsg(_('Tỉnh/TP < '.$SelectedCity.' > đã xóa.'),'success');
			}else{
				echo prnMsg(_('Tỉnh/TP < '.$SelectedCity.' > chưa xóa.'),'success');
			}				
			unset ($SelectedCity);
			unset($_GET['deleteCity']);
		}
		
		if(isset($_SESSION['success'])){
		   /* Registration was successful */
		   if($_SESSION['success']){
				echo prnMsg(_('Tỉnh/TP < '.$_SESSION['successname'].' > đã tạo thành công.'),'success');
		   }
		   /* Registration failed */
		   else{
			  echo prnMsg(_('Tỉnh/TP < '.$_SESSION['successname'].' > không thể tạo'),'success');
		   }
		   unset($_SESSION['success']);
		   unset($_SESSION['successname']);
		}
		
		if(isset($_SESSION['edit'])){
			unset($_SESSION['edit']);
			echo prnMsg(_('Tỉnh/TP < '.$_SESSION['editname'].' > cập nhật thành công'),'success');
			unset($_SESSION['editname']);
		}

		if($form->num_errors > 0){
		   //echo $form->num_errors." error(s) found";
		   echo $form->error("SqlErrorMessage");
		   echo $form->error("name");
		}
		
		echo "<form id='form' method='post' action='../controllers/cityregion_controller.php" . "?" . SID . "'";
		if (isset($SelectedCity)) {				
			$city = $cityModel->getCityById($SelectedCity);
			
			$_POST['cityName'] = $city['name'];
			$_POST['citySortNo'] = $city['sortNo'];
			
			echo "<input type=hidden name='SelectedCity' VALUE='" . $SelectedCity . "'>";	
			echo "<a href='" . $_SERVER['PHP_SELF'] . '?' . SID . "'><font face='Verdana' size='2' color=red>" . _('Quay về danh sách Tỉnh/TP') . "</font></a>";
			echo '<h3 id="addcity">Cập nhật '.$_POST['cityName'].'</h3><br/>';
		}else{
			echo '<h3 id="addcity">Thêm Tỉnh/TP</h3><br/>';
		}	
		if (!isset($_POST['cityCreateDate'])){		
			$_POST['cityCreateDate']= $today;
		}
		echo '<fieldset id="city">';
		echo '<legend>THÔNG TIN CHUNG</legend>';
		echo '<label for="cityName">Tên Tỉnh/TP<span class="require">(*)</span>: </label><input name="cityName" id="cityName" type="text" tabindex="1" size="70" value="'.(isset($SelectedCity)?$_POST['cityName']:$form->value("cityName")).'"/><br />';
		echo '<label for="citySortNo">Thứ tự hiển thị: </label><input name="citySortNo" id="citySortNo" type="text" tabindex="2" size="30" value="'.(isset($SelectedCity)?$_POST['citySortNo']:$form->value("citySortNo")).'"/><br />';
		if(!isset($SelectedCity)){
			$createDate = (!isset($SelectedCity)?$_POST['cityCreateDate']:$form->value("cityCreateDate"));
			echo '<label for="cityCreateDate">Ngày tạo: </label><input name="cityCreateDate" id="cityCreateDate" readonly="readonly" type="text" tabindex="7" size="15" value="'.$createDate.'"/>';
			echo '<a href="javascript:NewCal(\'cityCreateDate\', \'ddmmyyyy\', false, 24, \'..\');"><img border="0" src="../img/cal.jpg" alt="" /></a><br />';
			echo '<input type="hidden" name="cityexadd" value="1" />';
		}else{
			echo '<input type="hidden" name="cityexedit" value="1" />';
		}
		echo '</fieldset>';
		echo '<div align="center">
				<input name="submitCity" type="submit" value="Đồng ý" /> 
				<input name="resetsubmitCity" type="reset"  value="Thiết lập lại"/>				
			  </div></form>';
		
		echo '</div>';
		echo '<br />';
	}
	
	if (!isset($SelectedCity) && !isset($SelectedDistrict)) {
	echo '<div id="box">';
	
		if (isset($_GET['delete']) && isset($SelectedDistrict) && isset($SelectedCity)) {
			if($realestateModel->deleteType($SelectedDistrict, $SelectedCity) == 0){
				echo prnMsg(_('Loại BĐS < '.$SelectedDistrict.' > đã xóa.'),'success');
			}else{
				echo prnMsg(_('Loại BĐS < '.$SelectedDistrict.' > chưa xóa.'),'success');
			}				
			unset ($SelectedDistrict);
			unset($_GET['delete']);
		}
	

	
		$start=$_GET['start'];
		if(strlen($start) > 0 and !is_numeric($start)){
			echo "Data Error";
			exit;
		}		
		
		$eu = ($start - 0); 
		$limit = $number_record_perpage;                                 
		$this1 = $eu + $limit; 
		$back = $eu - $limit; 
		$next = $eu + $limit;
		
		$num = $cityModel->getCountCities();		
		$result = $cityModel->getCities($eu, $limit);
		
		echo '<h3>Danh sách Tỉnh/TP</h3>';
		echo '<table width="100%"><thead>';
			echo '<tr class="borderBottom">';
	        echo '<th><a href="#">ID<img src="'.$PathPrefix.'img/icons/arrow_down_mini.gif" width="16" height="16" align="absmiddle" /></a></th>';
	        echo '<th><a href="#">Tên</a></th>';	        
			echo '<th><a href="#">Thứ tự</a></th>';     
	        echo '<th><a href="#">Ngày tạo</a></th>';
			echo '<th><a href="#">Ngày cập nhật</a></th>';      
	        echo '<th><a href="#">Hành động</a></th>';
	        echo '</tr></thead>';
			
			while ($myrow = $cityModel->getArrayNameRegions($result)) {
				
				printf('<tbody><tr class="borderBottom">
						<td class="a-center">%s</td>							                    
	                    <td>%s</td>
						<td>%s</td>	                       
	                    <td>%s</td>
						<td>%s</td>        
	                    <td><a href="%sSelectedCity=%s"><img src="'.$PathPrefix.'img/icons/user_edit.png" title="Edit user" width="16" height="16" /></a>
							<a href="%sSelectedCity=%s&deleteCity=1"><img src="'.$PathPrefix.'img/icons/user_delete.png" title="Delete user" width="16" height="16" /></a></td>
	                    </tr></tbody>',
						$myrow['id'],
						$myrow['name'],
						$myrow['sortNo'],													
						ConvertSQLDate($myrow['createDate']),
						ConvertSQLDate($myrow['updateDate']),					
						$_SERVER['PHP_SELF'] . '?' . SID . '&',						
						$myrow['id'],
						$_SERVER['PHP_SELF'] . '?' . SID . '&',						
						$myrow['id']);
			}
			
		echo '</table>';
		if($num > $limit ){
			echo '<div id="pager">Trang ';
			if($back >=0) { 
				
				echo '<a href="'.$_SERVER['PHP_SELF'].'?'.'&start='.$back.'"><img src="'.$PathPrefix.'img/icons/arrow_left.gif" width="16" height="16" /></a>';
			};
			$i=0;
			$l=1;
			for($i=0;$i < $num;$i=$i+$limit){
				if($i <> $eu){
					echo " <a href='".$_SERVER['PHP_SELF']."?"."&start=$i'><font face='Verdana' size='2'>$l</font></a> ";
				}
				else { 
					echo "<font face='Verdana' size='4' color=red>$l</font>";				
				}
					$l=$l+1;
			}
			if($this1 < $num) {
				echo '<a href="'.$_SERVER['PHP_SELF'].'?'.'&start='.$next.'"><img src="'.$PathPrefix.'img/icons/arrow_right.gif" width="16" height="16" /></a>';
			};
            
			 echo ' Tổng cộng <strong>'.$num.'</strong> Tỉnh/TP
                    </div>';
					
		}else{
			echo '<div id="pager">Tổng cộng <strong>'.$num.'</strong> Tỉnh/TP</div>';
		}    
	
		echo '</div>';
		echo '<br />';
	}
	
	
	if (isset($_GET['delete']) && isset($SelectedCity) && isset($SelectedDistrict)) {
		if($cityModel->deleteDistrict($SelectedDistrict) == 0){
			echo prnMsg(_('Quận/Huyện < '.$SelectedDistrict.' > đã xóa.'),'success');
		}else{
			echo prnMsg(_('Quận/Huyện < '.$SelectedDistrict.' > chưa xóa.'),'success');
		}				
		unset ($SelectedDistrict);
		unset($_GET['delete']);
	}
		
	if (isset($SelectedCity) && !isset($SelectedDistrict)) {
		echo '<div id="box">';
			$start=$_GET['start'];
			if(strlen($start) > 0 and !is_numeric($start)){
				echo "Data Error";
				exit;
			}		
			
			$eu = ($start - 0); 
			$limit = $number_record_perpage;
			$this1 = $eu + $limit; 
			$back = $eu - $limit; 
			$next = $eu + $limit;
			
			$num = $cityModel->getCountDistrictsByCityId($SelectedCity);		
			$result = $cityModel->getDistrictsByCity($SelectedCity, $eu, $limit);
			
			echo '<h3>Danh sách Quận/Huyện</h3>';
			echo '<table width="100%"><thead>';
				echo '<tr class="borderBottom">';
				echo '<th width="10px"><a href="#">ID<img src="'.$PathPrefix.'img/icons/arrow_down_mini.gif" width="16" height="16" align="absmiddle" /></a></th>';
				echo '<th width="160px"><a href="#">Tên</a></th>';	        
				echo '<th width="1px"><a href="#">Thứ tự</a></th>';     
				echo '<th width="10px"><a href="#">Ngày tạo</a></th>';
				echo '<th width="10px"><a href="#">Ngày cập nhật</a></th>';      
				echo '<th width="20px"><a href="#">Hành động</a></th>';
				echo '</tr></thead>';
				
				while ($myrow = $cityModel->getArrayNameRegions($result)) {
					
					printf('<tbody><tr class="borderBottom">
							<td class="a-center">%s</td>							                    
							<td>%s</td>
							<td>%s</td>	                       
							<td>%s</td>
							<td>%s</td>        
							<td><a href="%sSelectedCity=%s&SelectedDistrict=%s"><img src="'.$PathPrefix.'img/icons/user_edit.png" title="Edit user" width="16" height="16" /></a>
								<a href="%sSelectedCity=%s&SelectedDistrict=%s&delete=1"><img src="'.$PathPrefix.'img/icons/user_delete.png" title="Delete user" width="16" height="16" /></a></td>
							</tr></tbody>',
							$myrow['id'],
							$myrow['name'],
							$myrow['sortNo'],													
							ConvertSQLDate($myrow['createDate']),
							ConvertSQLDate($myrow['updateDate']),					
							$_SERVER['PHP_SELF'] . '?' . SID . '&',
							$SelectedCity,
							$myrow['id'],
							$_SERVER['PHP_SELF'] . '?' . SID . '&',
							$SelectedCity,
							$myrow['id']);
				}
				
			echo '</table>';
			if($num > $limit ){
				echo '<div id="pager">Trang ';
				if($back >=0) { 
					
					echo '<a href="'.$_SERVER['PHP_SELF'].'?'.'&SelectedCity='. $SelectedCity .'&start='.$back.'"><img src="'.$PathPrefix.'img/icons/arrow_left.gif" width="16" height="16" /></a>';
				};
				$i=0;
				$l=1;
				for($i=0;$i < $num;$i=$i+$limit){
					if($i <> $eu){
						echo " <a href='".$_SERVER['PHP_SELF']."?". "&SelectedCity=". $SelectedCity ."&start=$i'><font face='Verdana' size='2'>$l</font></a> ";
					}
					else { 
						echo "<font face='Verdana' size='4' color=red>$l</font>";				
					}
						$l=$l+1;
				}
				if($this1 < $num) {
					echo '<a href="'.$_SERVER['PHP_SELF'].'?'. '&SelectedCity='. $SelectedCity .'&start='.$next.'"><img src="'.$PathPrefix.'img/icons/arrow_right.gif" width="16" height="16" /></a>';
				};
				
				 echo ' Tổng cộng <strong>'.$num.'</strong> Quận/Huyện
						</div>';
						
			}else{
				echo '<div id="pager">Tổng cộng <strong>'.$num.'</strong> Quận/Huyện</div>';
			}
		
		echo '</div>';
		echo '<br />';
	}
	echo '<div id="box">';
	
	if(isset($_SESSION['successDistrict'])){
	   /* Registration was successful */
	   if($_SESSION['successDistrict']){
	   		echo prnMsg(_('Quận/Huyện < '.$_SESSION['successdistrictname'].' > đã tạo thành công.'),'success');
	   }
	   /* Registration failed */
	   else{
	      echo prnMsg(_('Quận/Huyện < '.$_SESSION['successdistrictname'].' > không thể tạo'),'success');
	   }
	   unset($_SESSION['successDistrict']);
	   unset($_SESSION['successdistrictname']);
	}
	
	if(isset($_SESSION['editdistrict'])){
   		unset($_SESSION['editdistrict']);
   		echo prnMsg(_('Quận/Huyện < '.$_SESSION['editdistrictname'].' > cập nhật thành công'),'success');
		unset($_SESSION['editdistrictname']);
	}
	
	if($form->num_errors > 0){
	   //echo $form->num_errors." error(s) found";
	   echo $form->error("SqlErrorMessage");
	   echo $form->error("name");
	   echo $form->error("district");
	}
	
	echo "<form id='form' method='post' action='../controllers/cityregion_controller.php" . "?" . SID . "'";
	if (isset($SelectedCity) && isset($SelectedDistrict)) {
			
		$myrow = $cityModel->getDistrictById($SelectedDistrict);
		
		$_POST['rees_city_id'] = $myrow['rees_city_id'];
		$_POST['name'] = $myrow['name'];
		$_POST['sortNo'] = $myrow['sortNo'];
	
		echo "<input type=hidden name='SelectedCity' VALUE='" . $SelectedCity . "'>";
		echo "<input type=hidden name='SelectedDistrict' VALUE='" . $SelectedDistrict . "'>";
		echo "<a href='" . $_SERVER['PHP_SELF'] . '?' . SID . '&SelectedCity='. $SelectedCity . "'><font face='Verdana' size='2' color=red>" . _('Quay về danh sách Tỉnh/TP') . "</font></a>";		
		echo '<h3 id="adduser">Cập nhật '.$_POST['name'].'</h3><br/>';
	}else{
		echo '<h3 id="adduser">Thêm Quận/Huyện</h3><br/>';
	}

	if (!isset($_POST['createDate'])){		
		$_POST['createDate']= $today;
	}
	
	echo '<fieldset id="personal">';
    echo '<legend>THÔNG TIN CHUNG</legend>';
	$name = ((isset($SelectedCity)&&isset($SelectedDistrict))?$_POST['name']:$form->value("name"));
	$sortNo = ((isset($SelectedCity)&&isset($SelectedDistrict))?$_POST['sortNo']:$form->value("sortNo"));
	echo '<label for="name">Tên Quận/Huyện<span class="require">(*)</span>: </label><input name="name" id="name" type="text" tabindex="1" size="70" value="'.$name.'"/><br />';
	echo '<label for="sortNo">Thứ tự hiển thị: </label><input name="sortNo" id="sortNo" type="text" tabindex="2" size="30" value="'.$sortNo.'"/><br />';
	if (!isset($SelectedDistrict)) {
		$createDate = (!isset($SelectedDistrict)?$_POST['createDate']:$form->value("createDate"));
		echo '<label for="createDate">Ngày tạo: </label><input name="createDate" id="createDate" readonly="readonly" type="text" tabindex="7" size="15" value="'.$createDate.'"/>';
		echo '<a href="javascript:NewCal(\'createDate\', \'ddmmyyyy\', false, 24, \'..\');"><img border="0" src="../img/cal.jpg" alt="" /></a><br />';
		echo '<input type="hidden" name="districtadd" value="1" />';
	}else{
		echo '<input type="hidden" name="districtedit" value="1" />';
	}
	
	echo '<label for="type">Loại Tỉnh/TP<span class="require">(*)</span>: </label><select name="type" style="font-size: 11px;">';
			if(isset($SelectedDistrict))
				$type = (isset($SelectedDistrict)?$_POST['rees_city_id']:$form->value("type"));			
			if(isset($SelectedCity))
				$type = (isset($SelectedCity)?$SelectedCity:$form->value("type"));
			if(!isset($SelectedDistrict) && !isset($SelectedDistrict)){
				echo '<option selected="selected" value="0">- Chọn Tỉnh/TP -</option>';
			}
			$cityArray = $cityModel->getCities();
			while ($myrow = $cityModel->getArrayNameRegions($cityArray)) {
				if($type == $myrow[0])
					echo '<option selected="selected" value="'.$myrow[0].'">'.$myrow[1].'</option>';					
				else echo '<option value="'.$myrow[0].'">'.$myrow[1].'</option>';
			}
        echo '</select><br />';	
	echo '</fieldset>';
	echo '<div align="center">
       		<input name="submit" type="submit" value="Đồng ý" /> 
            <input name="reset" type="reset"  value="Thiết lập lại"/>
          </div></form>';
	echo '</div>';		 
	echo '</div>';
	echo '</div>';
	
	include('../includes/footer.inc');
?>